package com.yycx.module.bus.provider.handler;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusOrder;
import liquibase.pro.packaged.E;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 内勤订单列表
 *
 * @author zyf
 */
@Component("nqOrderListHandler")
public class NqOrderListHandler implements PageInterceptor<BusOrder> {


    @Autowired
    private TaskService taskService;


    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        Integer activeKey = params.getInt("activeKey", 0);
        if (activeKey.equals(0)) {
            cq.in("orderState", 0, 1);
        }
        if (activeKey.equals(2)) {
            cq.eq("orderState", 2);
        }
        if (activeKey.equals(3)) {
            cq.eq("orderState", 1);
        }
        if (activeKey.equals(1)) {
            cq.eq(true, "orderState", 2);
            cq.ne(true, "bod.customReturnState", 2);
            cq.eq(true, "bod.orderDetailsState", 1);
            cq.ne(true, "customBondType", 0);
        }
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Integer customBondType = entityMap.getInt("customBondType");
            Integer orderType = entityMap.getInt("orderType");
            Date sendDate = entityMap.get("sendDate");

            if (!customBondType.equals(0) && !customBondType.equals(7) && orderType.equals(1)) {
                Integer reputation = entityMap.getInt("reputation");
                if (FlymeUtils.isNotEmpty(sendDate)&&reputation<1000&&reputation>0) {
                    Date reputationDate = DateUtils.addDays(sendDate, reputation);
                    entityMap.put("reputationDate", DateUtils.formatDate(reputationDate, "yyyy-MM-dd"));
                    Integer dayNum = DateUtils.daysBetween(DateUtils.getNowDate(),reputationDate);
                    entityMap.put("dayNum", dayNum);
                } else {
                    entityMap.put("reputationDate", "");
                    entityMap.put("dayNum", -1);
                }
            } else {
                entityMap.put("reputationDate", "-");
                entityMap.put("dayNum", -1);
            }

            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }

}
